package com.example.guardian.myadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.guardian.R;

import java.util.ArrayList;
import java.util.List;

public class note_adapter extends BaseAdapter {
    List nameList;
    Context mContext;
    private LayoutInflater inflater;

    public note_adapter(List<ArrayList<String>> list, Context context){
        this.nameList = list;
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return nameList.size();
    }
    @Override
    public Object getItem(int position) {
        return nameList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            holder = new ViewHolder();
            //            由于我们只需要将XML转化为View，并不涉及到具体的布局，所以第二个参数通常设置为null
            convertView = inflater.inflate(R.layout.note_item,null);
            holder.kind_name = convertView.findViewById(R.id.kind_name);
            holder.target = convertView.findViewById(R.id.kind_target);
            //这句话的意思就是，让convertView与holder产生关联,通过set保存在Tag中
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
            //通过getTag，取出上面绑定的holder，以免多次绑定，造成资源浪费
        }
        ArrayList<String> bind = (ArrayList) nameList.get(position);
        holder.kind_name.setText((String) bind.get(0));
        holder.target.setText((String) bind.get(1));

        return convertView ;
    }

    public interface picListener {
        void onSpreadClick(int i);
    }

    //	声明接口
    private GridViewAdapter.picListener picListener;

    //	根据我们在回调里写的是 picListener 调用的onSpreadClick
    //	系统就会执行下面的方法
    public void picListener(GridViewAdapter.picListener picListener){
        this.picListener = picListener;
    }

    class ViewHolder {
        public TextView kind_name;
        public TextView target;

    }
}

